import codeApi from '@/api/code'
import { CODE_MAP } from '../constant'
import _ from 'underscore'

// initial state
const state = {
  actYearCodeList: [], // 活动年度
  actTypeCodeList: [], // 活动类型
  actStatusCodeList: [], // 活动状态
  actSortCodeList: [], // 活动序号(排序)
  actPuncloTypeList: [], // 打卡点类别
  bmColCodeList: [], // 活动报名表
  areaCodeList: [], // 区域
  costTypeCodeList: [], // 费用类型
  groupTypeCodeList: [], // 组别类型
  msgRecTypeCodeList: [], // 短信充值类型
  newsTypeCodeList: [] // 新闻类型
}

// getters
const getters = {
  actYearCodeList: state => state.actYearCodeList,
  actTypeCodeList: state => state.actTypeCodeList,
  actStatusCodeList: state => state.actStatusCodeList,
  actSortCodeList: state => state.actSortCodeList,
  actPuncloTypeList: state => state.actPuncloTypeList,
  bmColCodeList: state => state.bmColCodeList,
  areaCodeList: (state) => {
    return state.areaCodeList.map((item) => {
      let area = Object.assign({}, item)
      area.code = String(area.code)
      return area
    })
  },
  costTypeCodeList: state => state.costTypeCodeList,
  groupTypeCodeList: state => state.groupTypeCodeList,
  msgRecTypeCodeList: state => state.msgRecTypeCodeList,
  newsTypeCodeList: state => state.newsTypeCodeList
}

// actions
const actions = {
  // 获取字典
  async getCode ({ commit }, dictStr) {
    let type = CODE_MAP[dictStr]
    let sessionStorageCache = sessionStorage.getItem(type.flag)
    // 判断是有sessionStorage 缓存 有返回
    if (sessionStorageCache && sessionStorageCache.length > 0) {
      let list = JSON.parse(sessionStorageCache)
      commit(type.list, list)
      return list
    }
    // 判断 Vuex state 是否有缓存 有返回
    if (state[type.list].length > 0) {
      commit(type.list, state[type.list])
      return state[type.list]
    }
    const { body } = await codeApi.list({ codeType: type.code })
    let list = body.responseData
    // 如果数据正常，才存session
    if (list) {
      if (type.store === true) {
        sessionStorage.setItem(type.flag, JSON.stringify(list))
      }
      commit(type.list, list)
      return list
    }
  },
  // 更新字典
  async updateCodeByCode ({ commit }, code) {
    let type = _.findWhere(CODE_MAP, (item) => {
      return item.code === code
    })
    if (type) {
      const { body } = await codeApi.list({ codeType: type.code })
      let list = body.responseData
        // 如果数据正常，才存session
      if (list) {
        if (type.store === true) {
          sessionStorage.setItem(type.flag, JSON.stringify(list))
        }
        commit(type.list, list)
      }
    }
  }
}

// mutations
const mutations = {
  actYearCodeList (state, list) {
    state.actYearCodeList = list
  },
  actTypeCodeList (state, list) {
    state.actTypeCodeList = list
  },
  actStatusCodeList (state, list) {
    state.actStatusCodeList = list
  },
  actSortCodeList (state, list) {
    state.actSortCodeList = list
  },
  actPuncloTypeList (state, list) {
    state.actPuncloTypeList = list
  },
  bmColCodeList (state, list) {
    state.bmColCodeList = list
  },
  areaCodeList (state, list) {
    state.areaCodeList = list
  },
  costTypeCodeList (state, list) {
    state.costTypeCodeList = list
  },
  groupTypeCodeList (state, list) {
    state.groupTypeCodeList = list
  },
  msgRecTypeCodeList (state, list) {
    state.msgRecTypeCodeList = list
  },
  newsTypeCodeList (state, list) {
    state.newsTypeCodeList = list
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
